package com.bootdo.fx.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.*;
import com.bootdo.fx.domain.GoodsCategoryDO;
import com.bootdo.fx.service.GoodsCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.fx.domain.GoodsInfoDO;
import com.bootdo.fx.service.GoodsInfoService;

import javax.servlet.http.HttpServletResponse;

/**
 * @author 袁飞龙
 * @email 1992lcg@163.com
 * @date 2017-11-30 17:12:20
 */

@Controller
public class GoodsInfoController {
    @Autowired
    private GoodsInfoService goodsInfoService;
    @Autowired
    GoodsCategoryService goodsCategoryService;
    @Autowired
    private IdWorker idWorker;

    @GetMapping("/fx/goodsInfo")
    @RequiresPermissions("fx:goodsInfo:goodsInfo")
    String GoodsInfo() {
        return "fx/goodsInfo/goodsInfo";
    }

    @ResponseBody
    @GetMapping("/fx/goodsInfo/list")
    @RequiresPermissions("fx:goodsInfo:goodsInfo")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<GoodsInfoDO> goodsInfoList = goodsInfoService.list(query);
        int total = goodsInfoService.count(query);
        PageUtils pageUtils = new PageUtils(goodsInfoList, total);
        return pageUtils;
    }

    @GetMapping("/fx/goodsInfo/add")
    @RequiresPermissions("fx:goodsInfo:add")
    String add() {
        return "fx/goodsInfo/add";
    }

    @GetMapping("/fx/goodsInfo/edit/{goodsId}")
    @RequiresPermissions("fx:goodsInfo:edit")
    String edit(@PathVariable("goodsId") Long goodsId, Model model) {
        GoodsInfoDO goodsInfo = goodsInfoService.get(goodsId);
        GoodsCategoryDO goodsCategoryDO = goodsCategoryService.get(Long.parseLong(goodsInfo.getGoodsCategoryId()));
        goodsInfo.setGoodsCategoryId(goodsCategoryDO.getCategoryName());
        model.addAttribute("goodsInfo", goodsInfo);
        return "fx/goodsInfo/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/fx/goodsInfo/save")
    @RequiresPermissions("fx:goodsInfo:add")
    public R save(GoodsInfoDO goodsInfo) throws ParseException {
        goodsInfo.setGoodsId(idWorker.nextId() + "");
        goodsInfo.setGoodsCreateTime(DateUtils.parse(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN), DateUtils.DATE_TIME_PATTERN));
        if (goodsInfoService.save(goodsInfo) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/fx/goodsInfo/update")
    @RequiresPermissions("fx:goodsInfo:edit")
    public R update(GoodsInfoDO goodsInfo) throws ParseException {
        goodsInfo.setGoodsUpdateTime(DateUtils.parse(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN), DateUtils.DATE_TIME_PATTERN));
        goodsInfoService.update(goodsInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/fx/goodsInfo/remove")
    @ResponseBody
    @RequiresPermissions("fx:goodsInfo:remove")
    public R remove(Long goodsId) {
        if (goodsInfoService.remove(goodsId) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/fx/goodsInfo/batchRemove")
    @ResponseBody
    @RequiresPermissions("fx:goodsInfo:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] goodsIds) {
        goodsInfoService.batchRemove(goodsIds);
        return R.ok();
    }


    /*
    * 获取商品列表
    * */
    @RequestMapping("/wx/goodsInfo/listGoodsInfo")
    @ResponseBody
    public JsonResult listGoodsInfo(String categoryId, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String,Object> map = new HashMap<>();
        map.put("goodsIsShelves",1);
        if(categoryId != null && categoryId.length() >0){
            map.put("goodsCategoryId",categoryId);
            return ResultUtil.renderSuccess(goodsInfoService.list(map));
        }else{
            return ResultUtil.renderSuccess(goodsInfoService.list(map));
        }
    }

    /*
    * 获取热销商品列表
    * */
    @RequestMapping("/wx/goodsInfo/listHotGoods")
    @ResponseBody
    public JsonResult listHotGoods(HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String,Object> map = new HashMap<>();
        map.put("goodsIsHot",1);
        map.put("order","goods_order");
        return ResultUtil.renderSuccess(goodsInfoService.list(map));

    }

    /*
  * 获取热销商品列表
  * */
    @RequestMapping("/wx/goodsInfo/getGoodById")
    @ResponseBody
    public JsonResult getGoodById(String goodId,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        return ResultUtil.renderSuccess(goodsInfoService.get(Long.parseLong(goodId)));

    }

}
